<?php

/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Widget.php 5400 2010-09-12 17:53:28Z huuphuoc $
 * @since		2.0.9
 */
class Category_Widgets_Categories_Widget extends Tomato_Widget {

    protected function _prepareShow() {
        $config = Tomato_Config::getConfig();
        $isFeatured = $this->_request->getParam('is_featured');
        $startDepth = $this->_request->getParam('start_depth');
        $endDepth = $this->_request->getParam('end_depth');;
        $lang = $this->_request->getParam('lang', $config->web->language->code);
        $categoryModel = Rabbit_Model_Factory::getInstance()->setModule('category')->getCategoryModel();
        $categoryModel->setLang($lang);

        $categories = $categoryModel->getTree('product');
        if ($isFeatured != null || is_numeric($startDepth) || is_numeric($endDepth)) {
            
            for ($i = 0; $i < count($categories); $i++)
                if (
                        ($isFeatured && !$categories[$i]->featured)
                        || ($startDepth && $categories[$i]->depth < $startDepth)
                        || ($endDepth && $categories[$i]->depth > $endDepth)
                ) {
                    unset($categories[$i]);
                    continue;
                }
        }


        $this->_view->assign('categories', $categories);
    }

    protected function _prepareConfig() {
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
        $categoryModel = Rabbit_Model_Factory::getInstance()->setModule('category')->getCategoryModel();
        $categoryModel->setDbConnection($conn);
        $categories = $categoryModel->getTree();

        $this->_view->assign('categories', $categories);
    }

}
